const {
    jwtDecode
} = require('../utils/jwt')
const adminModel = require('../db/modle/adminModel')

module.exports = async (req, res, next) => {
    const {
        token
    } = req.headers

    const jwtResult = jwtDecode(token,'123')

    if (jwtResult.code === 0) {
        return res.send({
            code: 20000,
            msg: "服务端权限校验解码失败",
            data: null
        })
    }
    try {
        const findResult = await adminModel.findOne({
            adminName: jwtResult.adminName
        })
        if (findResult.status === 0) {
            return res.send({
                code: 20000,
                msg: '服务端权限校验失败,数据库保存的status异常',
                data: null
            })
        }

        //权限校验通过,继续向下运行
        next();
    } catch (e) {
        console.log("权限控制中:查询status状态出现异常" + e.message);
        res.send({
            code: 20000,
            msg: "权限控制中:查询status状态出现异常" + e.message,
            data: null
        })

    }
}